package com.jeasonzhao.commons.basic;

public abstract class IntegerPair implements java.io.Serializable
{
    private static final long serialVersionUID = 1L;
    public final static String ALIAS_DILIMITER = ",";
    private int m_nId = -1;
    private String m_strName = null;
    private String m_strAliasNames = null;
    protected IntegerPair()
    {

    }

    protected IntegerPair(int nId)
    {
        this.m_nId = nId;
    }

    protected IntegerPair(int nId,String strName,String ...strAlias)
    {
        this.m_nId = nId;
        this.m_strName = strName;
        setAliasName(strAlias);
    }

    protected IntegerPair(IntegerPair p)
    {
        if(null != p)
        {
            this.m_nId = p.m_nId;
            this.m_strAliasNames = p.m_strAliasNames;
            this.m_strName = p.m_strName;
        }
    }

    protected void setName(String name)
    {
        this.m_strName = name;
    }

    protected void setId(int nId)
    {
        this.m_nId = nId;
    }

    protected void setAliasName(String ...strAlias)
    {
        this.m_strAliasNames = null;
        for(String group : strAlias)
        {
            if(null == group || group.length() < 1)
            {
                continue;
            }
            else
            {
                java.util.HashSet<String> names = new java.util.HashSet<String>();
                for(String part : group.split(ALIAS_DILIMITER))
                {
                    if(part.trim().length() < 1
                       || names.contains(part.trim().toLowerCase()))
                    {
                        continue;
                    }
                    else
                    {
                        names.add(part.trim().toLowerCase());
                        if(null == m_strAliasNames)
                        {
                            m_strAliasNames = part;
                        }
                        else
                        {
                            m_strAliasNames += ALIAS_DILIMITER + part.trim().toLowerCase();
                        }
                    }
                }
            }
        }
    }

    public String getAliasNames()
    {
        return this.m_strAliasNames;
    }

    public boolean containsAliasName(String str)
    {
        if(null == m_strAliasNames || str == null || str.trim().length() < 1)
        {
            return false;
        }
        str = str.trim().toLowerCase();
        return
            m_strAliasNames.equals(str) ||
            m_strAliasNames.startsWith(str + ALIAS_DILIMITER) ||
            m_strAliasNames.endsWith(ALIAS_DILIMITER + str) ||
            m_strAliasNames.indexOf(ALIAS_DILIMITER + str + ALIAS_DILIMITER) >= 0;
    }

    public int hashCode()
    {
        return super.hashCode();
    }

    public int getId()
    {
        return m_nId;
    }

    public String getName()
    {
        return m_strName;
    }

    public boolean equals(int n)
    {
        return this.m_nId == n;
    }

    public boolean equals(IntegerPair type)
    {
        //Do not call method to get ID, but use the member directly.
        return type != null && this.m_nId == type.m_nId;
    }

    public boolean equals(Object obj)
    {
        if(null == obj)
        {
            return false;
        }
        if(obj instanceof IntegerPair)
        {
            return this.equals((IntegerPair) obj);
        }
        else
        {
            return super.equals(obj);
        }
    }

    public String toString()
    {
        String strName = this.m_strName;
        if(null == strName || strName.trim().length() < 1)
        {
            strName = this.getConstantFieldName();
        }
        return "{" + this.m_nId + "=" + strName + "}";
    }

    public String getConstantFieldName()
    {
        return IntegerPairHelper.getConstantFieldName(this);
    }

    public final static IntegerPair findConstant(Class<?> cls,int nIndex)
    {
        return IntegerPairHelper.find(cls,nIndex);
    }

    public final static IntegerPair findConstant(Class<?> cls,String strName,IntegerPair defaultValue)
    {
        return IntegerPairHelper.find(cls,strName,defaultValue);
    }

    public final static IntegerPair findConstant(Class<?> cls,String strName)
    {
        return IntegerPairHelper.find(cls,strName,null);
    }

}
